package com.wlsk.license.controller.common;


import com.wlsk.license.cache.DictListCacheService;
import com.wlsk.license.common.result.Result;
import com.wlsk.license.common.utils.ValidationUtils;
import com.wlsk.license.dto.request.Key2ReqDto;
import com.wlsk.license.dto.request.KeyReqDto;
import com.wlsk.license.dto.request.UserReqDto;
import com.wlsk.license.dto.response.DictDataResultDto;
import com.wlsk.license.dto.response.feign.OrgTreeResVo;
import com.wlsk.license.service.ISysDictService;
import com.zhongjinfeng.util.helper.StringHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "通用接口")
@RequestMapping("/common")
public class CommonManageController {

    @Resource
    private ISysDictService sysDictService;

    @Resource
    private DictListCacheService dictListCacheService;

    @ApiOperation(value = "查询字典数据(集合)")
    @PostMapping("/dict/list")
    public Result<?> listDict(@RequestBody KeyReqDto reqDto) {
        if (StringHelper.isEmpty(reqDto.getKeywords())) {
            return Result.success(sysDictService.getAllGroupedByType());
        } else {
            return Result.success(dictListCacheService.get(reqDto.getKeywords()));
        }
    }

    @ApiOperation(value = "查询字典数据(单个)")
    @PostMapping("/dict/get")
    public Result<DictDataResultDto> getDict(@RequestBody KeyReqDto reqDto) {
        ValidationUtils.validate(reqDto);
        return Result.success(sysDictService.getOneByType(reqDto.getKeywords()));
    }

    @ApiOperation(value = "根据类型和值查询字典数据")
    @PostMapping("/dict/getByTypeValue")
    public Result<DictDataResultDto> getDict(@RequestBody Key2ReqDto reqDto) {
        ValidationUtils.validate(reqDto);
        return Result.success(sysDictService.getOneByTypeAndValue(reqDto.getDicType(), reqDto.getDicValue()));
    }

    @ApiOperation(value = "查询条件的机构列表")
    @PostMapping("/orgList")
    public Result<List<OrgTreeResVo>> orgList(@RequestBody UserReqDto reqDto) {
        return Result.success(sysDictService.orgList(reqDto));
    }

    @ApiOperation(value = "清楚Org缓存")
    @GetMapping("/clearOrgCache")
    public Result<String> clearOrgCache(String uid) {
        sysDictService.clearOrgCache(uid);
        return Result.success("删除成功");
    }
}
